// --------------------------------------------------------------------
//   name 	: 
//   desc 	: 
//   version 	: 
//   dev 	: 
// --------------------------------------------------------------------
// DEPENDENCIES
// 
// --------------------------------------------------------------------
// CHANGE HISTORY
// 0.1 	Initial Release
// --------------------------------------------------------------------
	plugins.addToolbar = { // Add the premium-like toolbar

		'enhance_game' : function() { // Add the premium-like toolbar
 		//make sure twe.curVillage is initalized
			personals.getCurrentVillage();

			if (/t=([^&]+)/.test(location.href)) {
				var t = '&' + location.href.match(/t=([^&]+)/g)
			} else {
					var t = '';
			}

			var menu_xhtml = '';
            
			menu_xhtml += '<td><a href="/game.php?village=' + twe.curVillage + '&screen=overview_villages'+t+'"><img src="' + personals.getImageBase() + 'unit/unit_snob.png" style="margin:0px 2px" title=" ' + twe.lang("villages") + ' " /></a></td>';
			
			// Add villages dropdown
			var vils = twe.myVillages();
			var multivillage=false;
			for(var v in vils) { if(v!=twe.curVillage) { multivillage = true; break; } }
			if(multivillage && twe.myVillages().length > 1) {
                        // Switch function for the dropdown
				setFunc("switch_village", function(selectObj){
					var target_village_id = selectObj.options[selectObj.selectedIndex].value;
					var redirect_to;
					if(target_village_id == ""){
					
						redirect_to = location.href.replace(/village=([^&]+)/, "village=" + twe.curVillage);
						redirect_to = location.href.replace(/page=([^&]+)/, "");
						redirect_to = location.href.replace(/screen=([^&]+)/, "screen=overview_villages&page=-1");
                                       // redirect_to = "/game.php?village=" + twe.curVillage + "&screen=overview_villages&page=-1";
					}else{
						redirect_to = location.href.replace(/village=([^&]+)/, "village=" + target_village_id);
					}
					location.href = redirect_to;
				});

				menu_xhtml += '<td><select onchange="switch_village(this)"><option value="">- - - - -</option>';

				var grps = personals.villageGroups();

				var tmp_menu_xhtml = '';
				var villist = new Array();

				for (x = 0; x < grps.length; x++) {
					eval("var grp = "+grps[x]);
					
					var option = '';
					
					for(var ij in vils) {
						var vil = vils[ij];
						if (vil['group'] == grp['id']) {
							option += '<option value="' + vil["id"] + '"' + (twe.curVillage == vil["id"] ? ' selected="selected"' : '') + '>' + (vil['alias'] != '' ? decodeURIComponent(vil['alias']) : decodeURIComponent(vil["name"])) + ' [' + vil["points"] + '] ' + vil["x"]+'|'+vil["y"] + '&nbsp;</option>';
							villist.push(vil['id']);
						}
					}
					if (option != '') {
					tmp_menu_xhtml += '<optgroup label="'+grp['name']+'">'; // group optgroup open
					tmp_menu_xhtml += option;
					tmp_menu_xhtml += '</optgroup>'; // group optgroup close
					}
				}

				if (villist.length != vils.length) {
					menu_xhtml += '<optgroup label="'+twe.lang("overview_nogroup")+'">';
				}

				for(var ij in vils) {
					var vil = vils[ij];
					if (villist.indexOf(vil['id']) == -1) {
						if (new String(vil['id']) != 'undefined') {
						menu_xhtml += '<option value="' + vil["id"] + '"' + (twe.curVillage == vil["id"] ? ' selected="selected"' : '') + '>' + (vil['alias'] != '' ? decodeURIComponent(vil['alias']) : decodeURIComponent(vil["name"])) + ' [' + vil["points"] + '] ' + vil["x"]+'|'+vil["y"] + '&nbsp;</option>';
						}
					}
				}
				if (villist.length != vils.length) {
					menu_xhtml += '</optgroup>'; // ungrouped optgroup close
				}
				menu_xhtml += tmp_menu_xhtml;
				menu_xhtml += '</select></td>';
			}

			var TW_Image_Base = personals.getImageBase();

            // Add links
			menu_xhtml += '<td><a href="/game.php?village=' + twe.curVillage + '&screen=overview'+t+'"><img src="' + TW_Image_Base + 'face.png" style="margin:0px 2px" title=" ' + twe.lang("overview") + ' " /></a></td>';
			menu_xhtml += '<td><a href="/game.php?village=' + twe.curVillage + '&screen=map'+t+'"><img src="' + TW_Image_Base + 'ally_rights/found.png" style="margin:0px 2px" title=" ' + twe.lang("map") + ' " /></a></td>';
			menu_xhtml += '<td style="border-left:dotted 1px"><a href="/game.php?village=' + twe.curVillage + '&screen=main'+t+'"><img src="' + TW_Image_Base + 'buildings/main.png" style="margin:0px 2px" title=" ' + twe.lang("main") + ' " /></a></td>';
			menu_xhtml += '<td><a href="/game.php?village=' + twe.curVillage + '&screen=barracks'+t+'"><img src="' + TW_Image_Base + 'buildings/barracks.png" style="margin:0px 2px" title=" ' + twe.lang("barracks") + ' " /></a></td>';
			menu_xhtml += '<td><a href="/game.php?village=' + twe.curVillage + '&screen=stable'+t+'"><img src="' + TW_Image_Base + 'buildings/stable.png" style="margin:0px 2px" title=" ' + twe.lang("stable") + ' " /></a></td>';
			menu_xhtml += '<td><a href="/game.php?village=' + twe.curVillage + '&screen=garage'+t+'"><img src="' + TW_Image_Base + 'buildings/garage.png" style="margin:0px 2px" title=" ' + twe.lang("garage") + ' " /></a></td>';
			if(twe.Premium){
				menu_xhtml += '<td><a href="/game.php?village=' + twe.curVillage + '&screen=smith'+t+'"><img src="' + TW_Image_Base + 'buildings/smith.png" style="margin:0px 2px" title=" ' + twe.lang("smith") + ' " /></a></td>';
				menu_xhtml += '<td style="border-left:dotted 1px"><a href="/game.php?village=' + twe.curVillage + '&screen=train'+t+'"><img src="' + TW_Image_Base + 'unit/unit_sword.png" style="margin:0px 2px" title=" ' + twe.lang("train") + ' " /></a></td>';
			}else{
				menu_xhtml += '<td style="border-left:dotted 1px"><a href="/game.php?village=' + twe.curVillage + '&screen=smith'+t+'"><img src="' + TW_Image_Base + 'buildings/smith.png" style="margin:0px 2px" title=" ' + twe.lang("smith") + ' " /></a></td>';
			}
			menu_xhtml += '<td><a href="/game.php?village=' + twe.curVillage + '&screen=place'+t+'"><img src="' + TW_Image_Base + 'buildings/place.png" style="margin:0px 2px" title=" ' + twe.lang("place") + ' " /></a></td>';
			menu_xhtml += '<td><a href="/game.php?village=' + twe.curVillage + '&screen=market'+t+'"><img src="' + TW_Image_Base + 'buildings/market.png" style="margin:0px 2px" title=" ' + twe.lang("market") + ' " /></a></td>';
			menu_xhtml += '<td><a href="/game.php?village=' + twe.curVillage + '&screen=snob'+t+'"><img src="' + TW_Image_Base + 'buildings/snob.png" style="margin:0px 2px" title=" ' + twe.lang("snob") + ' " /></a></td>';
			menu_xhtml += '<td style="border-left: dotted 1px"><a id="twtools_a" href="javascript:tools_show()"><img id="twtools_img" src="' + TW_Image_Base + 'unten.png" style="margin:0px 2px" title="Ext. Tools" /></a></td>';

			setVar("twtools_hidden", "yes");
			setFunc("tools_show", function(){
				window.setTimeout(function() {
					if(getVar("twtools_hidden") == "yes"){
						$("#twtools_div").slideToggle("slow");
						$("#twtools_img")[0].src = personals.getImageBase()+"oben.png";
						$("#twtools_tribe")[0].focus();
						$("#twtools_tribe")[0].value = twe.lang("tribe");
						$("#twtools_tribe")[0].select();
						setVar("twtools_hidden", "no");
					}else{
						$("#twtools_div").slideToggle("slow");
						$("#twtools_img")[0].src = "graphic/unten.png";
						setVar("twtools_hidden", "yes");
					}
				}, 0);
			});

			TWStatsPlayerColor=getValuePlugin('TWStatsPlayerColor');
			TWStatsTribeColor=getValuePlugin('TWStatsTribeColor');
			TWStatsGridShow=getValuePlugin('TWStatsGrid');
			TWStatsZoom=getValuePlugin('TWStatsZoom');
			TWStatsCenterX=getValuePlugin('TWStatsCenterX');
			TWStatsCenterY=getValuePlugin('TWStatsCenterY');
	
			TWTribeName = getValuePlugin('TribeShortName');
			TWTribeColor = getValuePlugin('TWStatsUserTColor');
			TWPlayerName = getValuePlugin('PlayerName');
			TWPlayerColor = getValuePlugin('TWStatsUserTColor');
			TWShowPlayer = getValuePlugin("ShowYouAndTribe");
			
			setFunc("tool_info", function(type,maporstats){

				var i_obj = $("#twtools_"+type)[0];
				var tribe_name = i_obj.value;
				if(tribe_name=="" || tribe_name==twe.lang(type)){
					i_obj.value = "";
					i_obj.focus();
					return;
				}
				var Tribes = document.getElementById("twtools_tribe").value;
				var Players = document.getElementById("twtools_player").value;
				var Tribes2 = Tribes.replace(/;/g,",").split(",");
				var Players2 = Players.replace(/;/g,",").split(",");
				if(Tribes2==twe.lang("tribe")) Tribes2="";
				if(Players2==twe.lang("player")) Players2="";
				if(Tribes2.constructor.toString().indexOf("Array") < 0) Tribes2 = new Array(Tribes2);
				if(Players2.constructor.toString().indexOf("Array") < 0) Players2 = new Array(Players2);
				var ExtraList = "";
				if(TWShowPlayer.toString()=="true") {
					ExtraList += "&tribe[][id]="+escape(TWTribeName)+"&tribe[][colour]="+TWTribeColor+"&player[][name]="+escape(TWPlayerName)+"&player[][colour]="+TWPlayerColor;
				}
				if(maporstats=="map") {
					for(var i=0;i<Tribes2.length;i++) {
						ExtraList += "&tribe[][id]="+escape(Tribes2[i])+"&tribe[][colour]="+TWStatsTribeColor;
					}
					for(var j=0;j<Players2.length;j++) {
						ExtraList += "&player[][name]="+escape(Players2[j])+"&player[][colour]="+TWStatsPlayerColor;
					}
					window.open( "http://www.twstats.com/"+twe.World+"/map.php?save=0&page=map&zoom="+TWStatsZoom+"&x="+TWStatsCenterX+"&y="+TWStatsCenterY+"&fill=000000&grid="+TWStatsGridShow+""+ExtraList );
				} else {
					if (type == 'player') {
					//	window.open("http://" + twe.World + ".tw-tools.com/index.php?name=" + escape(tribe_name) + "&search_at=tribe&object=stat&method=tribe_file&lng=en");
						window.open("http://www.twstats.com/"+twe.World+"/index.php?page=rankings&mode=players&searchstring="+escape(Players2[0]));
					} else {
					//	window.open("http://" + twe.World + ".tw-tools.com/index.php?name=" + escape(ally_name) + "&search_at=ally&object=stat&method=ally_file&lng=en");        
						window.open("http://www.twstats.com/"+twe.World+"/index.php?page=rankings&mode=tribes&searchstring="+escape(Tribes2[0]));
					}
				}
			});

                // Premium account
			var quickbar = $("quickbar");
			if(quickbar.length > 0) {
                        // Hide quickbar
				quickbar[0].style.display = "none";
			}
			try {
				$("#menu_row2")[0].innerHTML = menu_xhtml;
				var cells = $("#menu_row2 td");
 
				var twt_xhtml = '<tr><td><div id="twtools_div" class="twtools_div" style="display: none;">';
				twt_xhtml += '<table><tr>';
				twt_xhtml += '<td><a href="http://www.twstats.com/'+twe.World+'/index.php" target="_blank">TW-Stats</a>:</td>';
				twt_xhtml += '<td><input type="text" id="twtools_player" value="' + twe.lang("player") + '" /><a href="javascript:tool_info(\'player\',\'map\')"><img src="' + personals.getImageBase() + 'ally_rights/found.png" style="margin:0px 2px;width:12px;height:12px;" title="Show player village map" /></a><a href="javascript:tool_info(\'player\',\'stats\')"><img src="' + personals.getImageBase() + 'rechts.png" style="margin:0px 2px" title="' + twe.lang("i_player") + '" /></a></td>';//onfocus="i_focus(this)" onblur="i_blur_tribe(this)"
				twt_xhtml += '<td><input type="text" id="twtools_tribe" value="' + twe.lang("tribe") + '" /><a href="javascript:tool_info(\'tribe\',\'map\')"><img src="' + personals.getImageBase() + 'ally_rights/found.png" style="margin:0px 2px;width:12px;height:12px;" title="Show tribe village map" /></a><a href="javascript:tool_info(\'tribe\',\'stats\')"><img src="' + personals.getImageBase() + 'rechts.png" style="margin:0px 2px" title="' + twe.lang("i_tribe") + '" /></a></td>';//onfocus="i_focus(this)" onblur="i_blur_ally(this)" 
				twt_xhtml += '</tr></table>';
				twt_xhtml += '</div></td></tr>';
 
				$("#menu_row2")[0].parentNode.parentNode.parentNode.parentNode.parentNode.innerHTML += twt_xhtml;			
			} catch(e) {
				log(1, "Failed to execute overall.addToolbar properly, js error: \n"+e);
			}
		},
  'info' : {
	  'mandatory' : false,
   'name' : "Quick access toolbar + TW Stats tools",
   'desc' : "Add a toolbar to the game to allow quick access to your villages and their specifics",
   'version' : "0.1",
   'dev' : "Timas"
  },
  'variables' : {
    'tooltip':["","","","","","","","","","Remember this field is optional. And this is not phising for your data.. Its TWE anyway.","Remember this field is optional. And this is not phising for your data.. Its TWE anyway."],
	'desc':["Enemy Player/s villages color (hex value without #)","Enemy Tribe/s villages color (hex value without #)","Your villages color (hex value without #)","Your Tribe villages color (hex value without #)","Show/Hide the grid on the map","Zoom level (something inbetween 100-1000+)","X.Y Co-ordinates to center map on (one of your villages preferably)","X.Y Co-ordinates to center map on (one of your villages preferably)","Check this to show you and your tribe on the map.","Your tribe short name for eg: -0-","Your user/screen name for the game."],
	'TWStatsPlayerColor':"00FFFF",
	'TWStatsTribeColor':"FF0000",
	'TWStatsUserPColor':"FFFFFF",
	'TWStatsUserTColor':"FFFF00",
	'TWStatsGrid':["on","off"],
	'TWStatsZoom':"100",
	'TWStatsCenterX':"500",
	'TWStatsCenterY':"500",
	'ShowYouAndTribe':true,
	'TribeShortName':"-0-",
	'PlayerName':"Unknown"
  }
	};
